﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.ObjectModel;

namespace Freshome.m.fox
{
    public static class Kitools
    {
        public static T FindParent<T>(DependencyObject outerObj) where T : DependencyObject
        {
            DependencyObject obj = VisualTreeHelper.GetParent(outerObj);
            if (obj == null) return null;
            if (obj is T) return obj as T;

            while ((obj = VisualTreeHelper.GetParent(obj)) != null)
            {
                if (obj is T) return obj as T;
            }
            return null;
        }
        // Refresh local values after loading
        public static void Refresh<Top, T>(IEnumerable<Top> new_s, ObservableCollection<T> old_s)
            where T : mds.Shortype<Top>, new()
            where Top : Rmt.Shortype
        {
            for (int i = old_s.Count - 1; i >= 0; i--)
            {
                if (old_s[i].Created) continue;
                var raw = new_s.FirstOrDefault(m => m.Mid == old_s[i].Raw.Mid);
                if (raw != null) old_s[i].Refresh(raw);
                else old_s.RemoveAt(i);
            }
            foreach (var raw in new_s)
            {
                var row = old_s.FirstOrDefault(m => m.Raw.Mid == raw.Mid);
                if (row == null)
                {
                    row = new T();
                    row.Raw.Mid = raw.Mid;
                    row.Refresh(raw);
                    old_s.Add(row);
                }
            }
        }
        // Replace local values after updated or created
        public static void Replace<T, Top>(Dictionary<short, string> rts, IEnumerable<T> mrs, string titlead) 
            where T : mds.Shortype<Top>
            where Top : Rmt.Shortype
        {
            foreach (var mr in mrs)
            {
                if (mr.Created)
                {
                    if(rts.ContainsValue(mr.Stamp)) mr.Mid = rts.First(r => r.Value == mr.Stamp).Key;
                    else
                    {
                        if (rts.ContainsKey(mr.Mid) && mr.Mid < default(short))
                        {
                            MessageBox.Show(string.Format("{0}添加失败！\n{1}", mr.Stamp, rts[mr.Mid]), titlead, MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        else MessageBox.Show(string.Format("{0}添加失败！", mr.Stamp), titlead, MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                }
                else
                {
                    if (rts.ContainsKey(mr.Mid) && mr.Mid > default(short))
                    {
                        if (rts[mr.Mid] == mr.Stamp) mr.Updated = false;
                        else MessageBox.Show(string.Format("{0}修改失败！\n{1}", mr.Stamp, rts[mr.Mid]), titlead, MessageBoxButton.OK, MessageBoxImage.Error);
                    }
                    else MessageBox.Show(string.Format("{0}修改失败！", mr.Stamp), titlead, MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
    }
}
